﻿
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace StarWarsGame
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Textbox : DrawableGameComponent
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public Rectangle textBox;       
        SpriteFont font;
        SpriteFont LabelFont;

        String text;
        public String parsedText;
        String typedText;
        double typedTextLength;
        int delayInMilliseconds;
        bool isDoneDrawing;
        Game currentGame;
        Texture2D borderTexture;
        string LabelText;
        Keys oldKey;
        double lastInputTextTime;
        string defValue;
        int MaxLenght;

        public Textbox(Game game, SpriteFont currentfont, SpriteFont labelFont, SpriteBatch currentSpriteBatch, int xPos, int yPos, int height, int width, Texture2D border, string labelText, string defText, int maxLength)
            : base(game)
        {
            textBox = new Rectangle(xPos, yPos, width, height); 
           
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = currentSpriteBatch;
                   
            defValue = defText;
            font = currentfont;
            text = defText;//"This is a properly implemented word wrapped sentence in XNA using SpriteFont.MeasureString. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z";
            parsedText = parseText(text);
            delayInMilliseconds = 50;
            isDoneDrawing = false;
            typedText = parsedText;
            currentGame = game;
            borderTexture = border;
            LabelText = labelText;
            LabelFont = labelFont;
            MaxLenght = maxLength;
        }     
      
        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {           
        }    
   
        public override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            KeyboardState keys = Keyboard.GetState();

            Keys[] aKeys = keys.GetPressedKeys();
            if (aKeys.Length > 0)
            {
                double currentTime = gameTime.TotalGameTime.Seconds + gameTime.TotalGameTime.Milliseconds / 1000.0f;

                if (aKeys[0] != oldKey || currentTime > lastInputTextTime + 0.2)
                {
                    oldKey = aKeys[0];
                    lastInputTextTime = gameTime.TotalGameTime.Seconds + gameTime.TotalGameTime.Milliseconds / 1000.0f; ;
                    //foreach (Keys key in aKeys)
                    //{

                    Keys key = aKeys[0];
                    if (key == Keys.Back)
                    {
                        if (parsedText.Length > 0)
                        {
                            parsedText = parsedText.Substring(0, parsedText.Length - 1);
                        }
                        else
                        {
                            parsedText = "";
                        }
                    }

                    else if (parsedText.Length < MaxLenght)
                    {

                        if (key == Keys.Enter ||
                            key == Keys.Tab ||
                            key == Keys.Up ||
                            key == Keys.Down ||
                            key == Keys.Left ||
                            key == Keys.Right ||
                            key == Keys.OemComma
                            )
                        {
                            // parsedText = parsedText + "\r\n";
                        }

                        else if (key == Keys.D1)
                        {
                            parsedText = parsedText + "1";
                        }
                        else if (key == Keys.D2)
                        {
                            parsedText = parsedText + "2";
                        }
                        else if (key == Keys.D3)
                        {
                            parsedText = parsedText + "3";
                        }
                        else if (key == Keys.D4)
                        {
                            parsedText = parsedText + "4";
                        }
                        else if (key == Keys.D5)
                        {
                            parsedText = parsedText + "5";
                        }
                        else if (key == Keys.D6)
                        {
                            parsedText = parsedText + "6";
                        }
                        else if (key == Keys.D7)
                        {
                            parsedText = parsedText + "7";
                        }
                        else if (key == Keys.D8)
                        {
                            parsedText = parsedText + "8";
                        }
                        else if (key == Keys.D9)
                        {
                            parsedText = parsedText + "9";
                        }
                        else if (key == Keys.D0)
                        {
                            parsedText = parsedText + "0";
                        }

                        else if (key == Keys.Space)
                        {
                            parsedText = parsedText + " ";
                        }
                        else if (key == Keys.OemPeriod)
                        {
                            parsedText = parsedText + ".";
                        }
                        else
                        {
                            if (parsedText == defValue)
                            {
                                parsedText = "";
                            }


                            parsedText = parsedText + key.ToString();

                        }
                    }
                }
                typedText = parsedText;
                //    }
            }        
            
            base.Update(gameTime);
        }


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {            
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);              

            //LabelText
            spriteBatch.DrawString(LabelFont, LabelText, new Vector2(textBox.X, textBox.Y - LabelFont.LineSpacing), Color.Red);
            spriteBatch.DrawString(font, typedText, new Vector2(textBox.X, textBox.Y), Color.White);
            if (this.Enabled)
            {
                spriteBatch.Draw(borderTexture, new Rectangle(textBox.X, textBox.Y, textBox.Width, 1), Color.WhiteSmoke);
                spriteBatch.Draw(borderTexture, new Rectangle(textBox.X, textBox.Y + textBox.Height, textBox.Width, 1), Color.WhiteSmoke);
            }

            //else
            //{
            //   // spriteBatch.DrawString(font, typedText, new Vector2(textBox.X, textBox.Y), Color.White);
            //    //spriteBatch.Draw(borderTexture, new Rectangle(textBox.X, textBox.Y, textBox.Width, 1), Color.Red);
            //    //spriteBatch.Draw(borderTexture, new Rectangle(textBox.X, textBox.Y + textBox.Height, textBox.Width, 1), Color.Red);
            //}

            spriteBatch.End();

            base.Draw(gameTime);
        }

        private String parseText(String text)
        {
            String line = String.Empty;
            String returnString = String.Empty;
            String[] wordArray = text.Split(' ');

            foreach (String word in wordArray)
            {
                if (font.MeasureString(line + word).Length() > textBox.Width)
                {
                    returnString = returnString + line + '\n';
                    line = String.Empty;
                }

                line = line + word + ' ';
            }

            return returnString + line;
        }

    }
}